 
function [frame] = SurfMapPlotsubroutine(setasfile,modelvar)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% THIS FUNCTION GENERATES THE FIGURE OF A SETAS MODEL VARIABLE AND RETURNS 
% THE FRAME FOR THE ANIMATION PRODUCTION.
% 
% INPUT  :  - setasfile : a setas netcdf file name
%           - modelvar  : a SETAS variable. Available options are 'temp or 
%                         'salt' 
% OUTPUT :  - frame     : image frame 
%        
% Author: Benedicte Pasquer, IMOS/eMII (http://imos.org.au/)
% email: benedicte.pasquer@utas.edu.au
% May 2013   
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
load MyColormap.mat

ncid = netcdf.open(setasfile,'NC_NOWRITE');

% GET HORIZONTAL GRID INFO
varid = netcdf.inqVarID(ncid,'latitude');
lat = netcdf.getVar(ncid,varid); 
varid = netcdf.inqVarID(ncid,'longitude');
lon = netcdf.getVar(ncid,varid); 
lon =lon';lat =lat';
  
switch modelvar
    case 'temp'
        Var4title ='Temperature [degC]'; 
        varid = netcdf.inqVarID(ncid,'temp');

    case 'salt'

        Var4title ='Salinity [ppt]';
        varid = netcdf.inqVarID(ncid,'salt');
end
    var = netcdf.getVar(ncid,varid,'double'); 
    var = permute(var,[2 1 3 4]);
    var = mean(var,4); % MONTHLY MEAN
    var = mean(var(:,:,21:22),3);% MEAN OVER FIRST 2 TOP LAYERS
    var = squeeze(var);% REMOVE THE SINGLETON DIMENSION
    var(var>1000) = NaN;
    
    netcdf.close(ncid)

% SET VARIOUS VARIABLE  AND FIGURE PROPERTIES ACCORDING TO VAR NAME

dash = regexp(setasfile,'-');
yr = setasfile(dash-4:dash-1);
mth = setasfile(dash+1:dash+2);

% FIGURE
h = plot_map(lon,lat,var);

%COLORBAR

set (gcf,'Colormap',mycmap)
switch modelvar
    case 'temp'
%FIGURE TITLE
        figtitle{1} = ['CSIRO SETAS model: monthly mean surface Temperature ',mth,'/',yr,'  ' ];
        figtitle{2} = '(average over first 2 meters) ';
    
%COLORBAR TITLE
        cbartitle = 'Temperature [degC]';
%CAXIS
     
        caxis([5 20]);
        cbarxtick =[5 8 11 14 17 20];
%OUTPUT FILE NAME
        filename = 'SSTsurf_anim.gif';
    
    case 'salt'

        figtitle{1} = ['CSIRO SETAS model: monthly mean surface Salinity ',mth,'/',yr,'  '];
        figtitle{2} = '(average over first 2 meters)';
        cbartitle = 'Salinity [ppt]';

        caxis([0 35]) ;
        cbarxtick =[0 5 10 15 20 25 30 35];
        filename = 'SSSsurf_anim.gif';

end 

hh = colorbar('h');
set(get(hh,'title'),'string',cbartitle,'fontsize',11)
set(hh,'Position',[0.2 .30  .30 .03],'Xtick',cbarxtick);
title(figtitle,'fontsize',14)

hold off 

% PREPARE FRAME FOR ANIMATION

drawnow
frame= getframe(h);